<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Player extends MY_Main {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{		
		$this->load->view('template/header');
		$this->load->view('player/main');
		//$this->load->view('template/footer');
	}
	
	public function createPlayer(){
		$this->load->model ('master/teams','team');
		
		$data["teams"] = $this->team->getAllTeam();
		$data["title"]  = "Add Player";
		$data["action"] = "add";
		
		$this->load->view('template/header');
		$this->load->view('player/form',$data);
		$this->load->view('template/footer');
	}
	
	public function doCreate(){
		$this->load->model ('master/players','player');
		
		$picture = '/uploads/players/'.$_POST["userfile_filename"];
		
		$name = $_POST['player_name'];
		$no = $_POST['player_no'];
		$position = $_POST['player_position'];
		$birth = date('Y-m-d', strtotime($_POST['player_birth']));		
		$gender = $_POST['player_gender'];
		$height = $_POST['player_height'];
		$weight = $_POST['player_weight'];
		$teamid = $_POST['player_team'];
		$enabled = isset($_POST['player_enabled'])?$_POST['player_enabled']:0;
		$this->player->create_player($name,$no,$position,$height,$weight,$birth,$gender,$picture,$teamid,$enabled);
		
		//return date('Y-m-d', strtotime($_POST['player_birth']));
	}
	
	public function editPlayer(){
		$this->load->model ('master/players','player');
		$this->load->model ('master/teams','team');
		
		$data["teams"] = $this->team->getAllTeam();
		$playerid = $_GET['playerid'];
							
		$data["player"] = $this->player->get_player($playerid);
		
		$data["title"]  = "Edit Player";
		$data["action"] = "edit";
		
		$this->load->view('template/header');
		$this->load->view('player/form',$data);
		$this->load->view('template/footer');
	}
	
	public function doEdit(){
		$this->load->model ('master/players','player');
	
		$playerid = $_POST['player_id'];
	
		if(isset($_POST["userfile_filename"]) && $_POST["userfile_filename"] != ""){
			
			$picture = '/uploads/players/'.$_POST["userfile_filename"];
		}else{
			$picture = $_POST['player_picture'];
		}
						
		$name = $_POST['player_name'];
		$no = $_POST['player_no'];
		$position = $_POST['player_position'];
		$birth = date('Y-m-d', strtotime($_POST['player_birth']));		
		$gender = $_POST['player_gender'];
		$height = $_POST['player_height'];
		$weight = $_POST['player_weight'];
		$teamid = $_POST['player_team'];
		$enabled = isset($_POST['player_enabled'])?$_POST['player_enabled']:0;
		$this->player->edit_player($playerid,$name,$no,$position,$height,$weight,$birth,$gender,$picture,$teamid,$enabled);
				
	}
	
	public function doDelete(){
		$this->load->model ('master/players','player');
	
		$playerid = $_POST['playerid'];
		$this->player->delete_player($playerid);
	
	}
	
	
	public function getallplayer(){
	
		$this->load->model ('master/players','player');
		
		$data = $this->player->getAllPlayer();
	
		header("Content-type: application/json");
		echo "{\"data\":" .json_encode($data). "}";
		
	}
		
}